package cn.edu.scau.cmi.qiuyaohua.client;


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Group root = new Org("根组织",null);
    private Org o = (Org) root;

    @Override
    public void start(Stage primaryStage) {

        VBox vBox = new VBox();
        HBox hBox = new HBox();
        GridPane gridPane = new GridPane();

        TextArea textArea = new TextArea();
        TextField textField1 = new TextField();
        TextField textField2 = new TextField();
        Image image = new Image("image/1.png");
        ImageView view = new ImageView(image);
        textArea.setEditable(false);
        textField1.setEditable(false);
        textField1.setText(o.getName());
        gridPane.setHgap(20);
        gridPane.setVgap(10);
        gridPane.add(new Label(" 姓名：邱耀华 "),0,0);
        gridPane.add(new Label(" 学号：201627010120"),1,0);
        gridPane.add(new Label(" 当前组织："),0,1);
        gridPane.add(textField1,1,1);
        gridPane.add(new Label(" 输入栏："),0,2);
        gridPane.add(textField2,1,2);
        Button bt1 = new Button("成员信息");
        Button bt2 = new Button("所有成员");
        Button bt3 = new Button("添加个人");
        Button bt4 = new Button("添加组织");
        Button bt5 = new Button("删除成员");
        Button bt6 = new Button("查看组织");
        Button bt7 = new Button("返回上级");
        gridPane.add(bt1,0,3);
        gridPane.add(bt2,1,3);
        gridPane.add(bt3,0,4);
        gridPane.add(bt4,1,4);
        gridPane.add(bt5,0,5);
        gridPane.add(bt6,1,5);
        gridPane.add(bt7,2,5);

        hBox.getChildren().addAll(view,gridPane);
        vBox.getChildren().addAll(hBox,textArea);

        bt1.setOnAction(event ->
        {
            String s = textField2.getText();
            Group t = o.getMember(s);
            if(t!=null)
                textArea.setText(t.getInfo());
            else textArea.setText("找不到该成员");
        });
        bt2.setOnAction(event ->
                textArea.setText(o.printMember())
        );
        bt3.setOnAction(event ->
        {
            String s = textField2.getText();
            if(s!=null)
            {
                if(s.compareTo("")!=0)
                    textArea.setText("新个人:"+s+" "+o.addMember(new Person(s,o)));
                else textArea.setText("个人名不能为空");
            }
            else textArea.setText("个人名不能为空");
        });
        bt4.setOnAction(event ->
        {
            String s = textField2.getText();
            if(s!=null)
            {
                if(s.compareTo("")!=0) {
                    Org t = new Org(s,o);
                    textArea.setText("新组织:" + s + " " + o.addMember(t));
                }else textArea.setText("组织名不能为空");
            }
            else textArea.setText("组织名不能为空");

        });
        bt5.setOnAction(event ->
        {
            String s = textField2.getText();
            if(s!=null)
            {
                if(o.delMember(s))
                {
                    textArea.setText("删除成员:"+s+" 成功！");
                }
                else textArea.setText("找不到该成员");
            }
            else textArea.setText("成员名不能为空");
        });
        bt6.setOnAction(event ->
        {
            String s = textField2.getText();
            if(s!=null)
            {
                Group t = o.getMember(s);
                if(t instanceof Org)
                {
                    setOrg((Org)t);
                    textField1.setText(o.getName());
                }
                else textArea.setText("找不到该组织");
            }
            else textArea.setText("组织名不能为空");
        });
        bt7.setOnAction(event ->
        {
            if(o.getName().compareTo("根组织")==0)
            {
                textArea.setText("已经是根组织");
            }
            else
            {
                setOrg(o.getParent());
                textField1.setText(o.getName());
            }
        });
        Scene scene = new Scene(vBox);
        primaryStage.setTitle("安全组合模式方法");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void setOrg(Org org)
    {
        o = org;
    }
}
